package com.example.vod.service.impl;

import java.io.InputStream;
import java.util.List;

import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.example.vod.service.VodService;
import com.example.vod.utils.InitVodClient;
@Service
public class VodServiceImpl implements VodService{

	@Override
	public String uploadVideo(MultipartFile file) {
		// TODO Auto-generated method stub
	   String accessKeyId = "LTAI4GJPGzBeJ5jNNniq6kAj";
	   String accessKeySecret = "P01LGdhFP3ghj7VZ8H1dZnTRPrPoRh";
	   String fileName = file.getOriginalFilename();
	   String videoId = null;
	   String title = fileName.substring(0,fileName.indexOf("."));
	   try {
		   InputStream inputStream = file.getInputStream();
		   UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);

	       UploadVideoImpl uploader = new UploadVideoImpl();
	       UploadStreamResponse response = uploader.uploadStream(request);
	       System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
	       if (response.isSuccess()) {
	           videoId = response.getVideoId();
	       } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
	           System.out.print("VideoId=" + response.getVideoId() + "\n");
	           System.out.print("ErrorCode=" + response.getCode() + "\n");
	           System.out.print("ErrorMessage=" + response.getMessage() + "\n");
	       }
				
			} catch (Exception e) {
				// TODO: handle exception
			}
	   
		return videoId;
	}

	@Override
	public void deleteVideo(String id) {
		// TODO Auto-generated method stub
		    DefaultAcsClient client = InitVodClient.initVodClient("LTAI4GJPGzBeJ5jNNniq6kAj", "P01LGdhFP3ghj7VZ8H1dZnTRPrPoRh");
		
		    DeleteVideoRequest request = new DeleteVideoRequest();
		    //支持传入多个视频ID，多个用逗号分隔
		    request.setVideoIds(id);
		    try {
				client.getAcsResponse(request);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	}

	@Override
	public void deleteVideos(List<String> ids) {
		DefaultAcsClient client = InitVodClient.initVodClient("LTAI4GJPGzBeJ5jNNniq6kAj", "P01LGdhFP3ghj7VZ8H1dZnTRPrPoRh");

		DeleteVideoRequest request = new DeleteVideoRequest();

		String str = StringUtils.join(ids.toArray(), ",");

		request.setVideoIds(str);
		try {
			DeleteVideoResponse response = client.getAcsResponse(request);
		} catch (ClientException e) {
			e.printStackTrace();
		}

	}


}
